package com.xuebang.admin.util;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static Integer getYear(Date date) {
		if (date != null) {
			return Date2Calendar(date).get(Calendar.YEAR);
		} else {
			return Calendar.getInstance().get(Calendar.YEAR);
		}
	}

	public static Integer getMonth(Date date) {
		if (date != null) {
			return Date2Calendar(date).get(Calendar.MONTH) + 1;
		} else {
			return Calendar.getInstance().get(Calendar.MONTH) + 1;
		}
	}

	public static Integer getDate(Date date) {
		if (date != null) {
			return Date2Calendar(date).get(Calendar.DATE);
		} else {
			return Calendar.getInstance().get(Calendar.DATE);
		}
	}

	public static Integer getHour(Date date) {
		if (date != null) {
			return Date2Calendar(date).get(Calendar.HOUR_OF_DAY);
		} else {
			return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		}
	}

	// 日期格式化
	public static String format(Calendar c, String pattern) {
		Calendar calendar = null;
		if (c != null) {
			calendar = c;
		} else {
			calendar = Calendar.getInstance();
		}
		if (pattern == null || pattern.equals("")) {
			pattern = "yyyy年MM月dd日 HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);

		return sdf.format(calendar.getTime());
	}

	public static Calendar Date2Calendar(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c;
	}

	public static Date Calendar2Date(Calendar c) {
		return c.getTime();
	}

	public static Date getCurrentTime() {
		return new Date();
	}

	// 将日期时间格式化为 yyyy-MM-dd HH:mm:ss
	public static String getDateTimeString(Date date) {
		return format(Date2Calendar(date), "yyyy-MM-dd HH:mm:ss");
	}

	// 将日期时间格式化为 yyyy-MM-dd
	public static String getDateString(Date date) {
		return format(Date2Calendar(date), "yyyy-MM-dd");
	}

	// 将日期时间格式化为 yyyy年-MM月-dd日
	public static String getLocalDateString(Date date) {
		return format(Date2Calendar(date), "yyyy年-MM月-dd日");
	}

	// 获取当天0点时间
	public static Date getStartTime() {
		Calendar c = Calendar.getInstance();
		Integer year = c.get(Calendar.YEAR);
		Integer month = c.get(Calendar.MONTH);
		Integer date = c.get(Calendar.DAY_OF_MONTH);
		c.set(year, month, date, 0, 0, 0);
		return Calendar2Date(c);
	}

	// 获取当天24点时间
	public static Date getEndTime() {
		Calendar c = Calendar.getInstance();
		Integer year = c.get(Calendar.YEAR);
		Integer month = c.get(Calendar.MONTH);
		Integer date = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(date + " = date ");
		c.set(year, month, date, 24, 0, 0);
		return Calendar2Date(c);
	}

	// 获取指定日期的指定小时时间
	public static Date getHourTime(Date date, Integer hourNum) {
		Calendar c = Date2Calendar(date);
		c.set(Calendar.HOUR_OF_DAY, hourNum);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return Calendar2Date(c);
	}

	// 两个日期求差
	public static Long getDateSubtract(Date firstDate, Date secondDate) {
		Long first = firstDate.getTime();
		Long second = secondDate.getTime();
		if (first > second) {
			return first - second;
		} else {
			return second - first;
		}
	}

	public static Long getDateSubtract(Date firstDate, Long secondLong) {
		Long first = firstDate.getTime();
		if (first > secondLong) {
			return first - secondLong;
		} else {
			return secondLong - first;
		}
	}

	// 保留指定位数的小数
	public static Double getDigitNum(double d, int digit) {
		BigDecimal bd = new BigDecimal(d);
		bd = bd.setScale(digit, BigDecimal.ROUND_HALF_UP);
		Double result = bd.doubleValue();
		return result;
	}

	// test
	public static void main(String[] args) {

	}

}
